package com.byedu.focus;

import com.byedu.base.BaseService;
import com.byedu.domain.focus.FocusKpiDO;
import com.byedu.domain.focus.FocusNoticeDO;
import com.byedu.domain.focus.FocusProjectDO;
import com.byedu.domain.focus.FocusUserDO;
import com.byedu.domain.focus.vo.FocusKpiVO;
import com.byedu.domain.focus.vo.FocusProjectVO;
import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.domain.focus.vo.JiduVO;
import com.byedu.mapper.focus.FocusKpiMapper;
import com.byedu.mapper.focus.FocusNoticeMapper;
import com.byedu.mapper.focus.FocusProjectMapper;
import com.byedu.mapper.focus.FocusUserMapper;
import com.byedu.util.CommonUtil;
import com.byedu.util.DateUtil;
import com.byedu.util.yunpian.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jason on 2017/5/14.
 */
@Service
public class FocusKpiService extends BaseService {

    @Autowired
    FocusKpiMapper focusKpiMapper;
    @Autowired
    FocusUserMapper focusUserMapper;
    @Autowired
    FocusProjectMapper focusProjectMapper;
    @Autowired
    FocusNoticeMapper focusNoticeMapper;
    /**
     * @description 更新
     * @author chenxigui
     * @date  2017-05-14
     * @param focusKpiDO
     * @return
     */
    public Integer updateFocusKpiDO(FocusKpiDO focusKpiDO){
        return focusKpiMapper.updateFocusKpiDObyId(focusKpiDO);
    }

    /**
     * @description 查询用户
     * @author chenxigui
     * @date  2017-05-14
     * @param userId
     * @return
     */
    public List<FocusKpiVO> getFocusKpiDOByFocusKpiDO(String userId,String jidu){
        FocusKpiDO searchKpiDO = new FocusKpiDO();
        if(jidu != null){
            String[] timeStr = jidu.split("_");
            searchKpiDO.setPryear(timeStr[0]);
            searchKpiDO.setPrjidu(timeStr[1]);
        }
        searchKpiDO.setUserid(userId);
        List<FocusKpiDO> focusKpiDOList =focusKpiMapper.getFocusKpiDOByFocusKpiDO(searchKpiDO);
        List<FocusUserDO> focusUserDOList = focusUserMapper.getFoucUserList();

        List<FocusKpiVO> focusKpiVOList = new ArrayList<FocusKpiVO>();
        for (FocusKpiDO focusKpiDO :focusKpiDOList){
            FocusUserVO focusUserVO = null;
            FocusProjectDO searchFocusProjectDO = new FocusProjectDO();
            searchFocusProjectDO.setCoid(focusKpiDO.getUserid());
            searchFocusProjectDO.setPryear(focusKpiDO.getPryear());
            searchFocusProjectDO.setPrjidu(focusKpiDO.getPrjidu());
           List<FocusProjectDO> projectList = focusProjectMapper.getFocusProjectDOKPI(searchFocusProjectDO);

            double  sumPo = 0.0;
            double  sumBill = 0.0;

            for(FocusProjectDO focusProjectDO : projectList){
                sumPo = sumPo+CommonUtil.stringToDouble(focusProjectDO.getPonum());
                sumBill = sumBill+CommonUtil.stringToDouble(focusProjectDO.getBillnum());
            }
            focusKpiDO.setDoneponum(CommonUtil.doubletoStr(sumPo));
            focusKpiDO.setBillnum(CommonUtil.doubletoStr(sumBill));
            focusKpiDO.setLendnum();
//            focusKpiDO.set
            for(FocusUserDO focusUserDO : focusUserDOList){
                if(focusKpiDO.getUserid() .equals(focusUserDO.getId())){
                    focusUserVO = new FocusUserVO(focusUserDO);
                    break;
                }
            }
            FocusKpiVO focusKpiVO =  new FocusKpiVO(focusKpiDO,focusUserVO);
            focusKpiVOList.add(focusKpiVO);
        }
        return  focusKpiVOList;
    }

    public Integer addFocusKpiDO(FocusKpiDO focusKpiDO){
     return  focusKpiMapper.addFocusKpiDO(focusKpiDO);
    }

    public Integer delFocusKpiDO(FocusKpiDO focusKpiDO){
        return  focusKpiMapper.delFocusKpiDObyId(focusKpiDO);
    }

    public List<FocusKpiVO> getFocusKpiVOList(){
        List<FocusKpiDO> focusKpiDOList =focusKpiMapper.getFocusKpiDOList();
        List<FocusUserDO> focusUserDOList = focusUserMapper.getFoucUserList();

        List<FocusKpiVO> focusKpiVOList = new ArrayList<FocusKpiVO>();
        for (FocusKpiDO focusKpiDO :focusKpiDOList){
            FocusUserVO focusUserVO = null;
            for(FocusUserDO focusUserDO : focusUserDOList){
                if(focusKpiDO.getUserid() .equals(focusUserDO.getId())){
                    focusUserVO = new FocusUserVO(focusUserDO);
                    break;
                }
            }
            FocusKpiVO focusKpiVO =  new FocusKpiVO(focusKpiDO,focusUserVO);
            focusKpiVOList.add(focusKpiVO);
        }
        return  focusKpiVOList;
    }

    public FocusKpiVO getFocusKpiDOById(String id) {
        FocusUserVO focusUserVO =null;
        FocusKpiDO focusKpiDO = focusKpiMapper.getFocusKpiDOById(id);
        List<FocusUserDO> focusUserDOList = focusUserMapper.getFoucUserList();
        FocusKpiVO focusKpiVO =  null;
        for(FocusUserDO focusUserDO : focusUserDOList){
            if(focusKpiDO.getUserid() .equals(focusUserDO.getId())){
                focusUserVO = new FocusUserVO(focusUserDO);
                break;
            }
        }
        focusKpiVO  =  new FocusKpiVO(focusKpiDO,focusUserVO);
        return focusKpiVO;
    }

    public FocusKpiDO getkpiListSum(List<FocusKpiVO> kpiList) {
        FocusKpiDO focusKpiDO = new FocusKpiDO();
        double  sumPlan = 0.0;
        double  sumReal = 0.0;
        double  sumBill = 0.0;
        for (FocusKpiVO focusKpiVO : kpiList){
            sumPlan = sumPlan+ CommonUtil.stringToDouble(focusKpiVO.getKipnum());
            sumReal = sumReal+ CommonUtil.stringToDouble(focusKpiVO.getDoneponum());
            sumBill = sumBill+ CommonUtil.stringToDouble(focusKpiVO.getBillnum());
        }
        //计划金额
        focusKpiDO.setKipnum(CommonUtil.doubletoStr(sumPlan));
        focusKpiDO.setDoneponum(CommonUtil.doubletoStr(sumReal));
        focusKpiDO.setBillnum(CommonUtil.doubletoStr(sumBill));
        focusKpiDO.setLendnum();

        return focusKpiDO;

    }

    public List<JiduVO> getjifuList() {
        List<FocusKpiDO> focusKpiDOs = focusKpiMapper.getjifuList();
        List<JiduVO> jiduVOList = new  ArrayList<JiduVO>();
        for(FocusKpiDO focusKpiDO : focusKpiDOs){
            JiduVO jiduVO = new JiduVO(focusKpiDO);
            jiduVOList.add(jiduVO);
        }

        return jiduVOList;
    }

    public List<String> getyearList() {
        List<FocusKpiDO> focusKpiDOs = focusKpiMapper.getyearList();
        List<String> jiduVOList = new  ArrayList<String>();
        for(FocusKpiDO focusKpiDO : focusKpiDOs){
            String year = focusKpiDO.getPryear();
            jiduVOList.add(year);
        }

        return jiduVOList;
    }

    public List<FocusKpiVO> getFocusKpiDOWeekByFocusKpiDO(String userId, Integer index) {
        FocusProjectDO searchProjectDO = new FocusProjectDO();
        searchProjectDO.setCoid(userId);
        List<FocusProjectDO> focusProjectDOlist = focusProjectMapper.getFocusProjectDOKPI(searchProjectDO);
        FocusNoticeDO focusNoticeDO = focusNoticeMapper.getFocusNotice();
        List<FocusKpiVO> focusKpiVOList = new ArrayList<FocusKpiVO>();
        List<FocusUserDO> focusUserDOList = focusUserMapper.getFoucUserList();
        Map<String ,FocusUserDO> stringMap = new HashMap<String ,FocusUserDO>();
        for(FocusProjectDO focusProjectDO : focusProjectDOlist){
//            System.out.println("billtime:"+focusProjectDO.getBilltime());
//            System.out.println("index:"+index);
            if(DateUtil.isSameWeekWithToday(DateUtil.plusDay(focusNoticeDO.getHuikuan(),focusProjectDO.getBilltime()),index)){

                for(FocusUserDO focusUserDO : focusUserDOList) {
                   if(focusUserDO.getId().equals(focusProjectDO.getCoid())) {
                       stringMap.put(focusProjectDO.getCoid(), focusUserDO);
                   }
               }
            }
        }
        for (String in : stringMap.keySet()) {
            FocusUserVO focusUserVO = new FocusUserVO(stringMap.get(in));
            FocusKpiVO focusKpiVO = new FocusKpiVO(null,focusUserVO);
            focusKpiVOList.add(focusKpiVO);
          }
        for(int i =0 ;i<focusKpiVOList.size();i++){
            for(FocusProjectDO focusProjectDO : focusProjectDOlist){
                if(DateUtil.isSameWeekWithToday(DateUtil.plusDay(focusNoticeDO.getHuikuan(),focusProjectDO.getBilltime()),index)){

                    if(focusProjectDO.getCoid().equals(focusKpiVOList.get(i).getUser().getId())){
                        System.out.println("--------"+true);
                        //计划回款金额 60天后
                        focusKpiVOList.get(i).setKipnum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getKipnum()) + CommonUtil.stringToDouble(focusProjectDO.getPonum())));
                        focusKpiVOList.get(i).setBillnum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getBillnum()) + CommonUtil.stringToDouble(focusProjectDO.getBillnum())));
                        if(!StringUtil.isNullOrEmpty(focusProjectDO.getRertime())){
                            //已经回款金额
                            focusKpiVOList.get(i).setDoneponum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getDoneponum()) + CommonUtil.stringToDouble(focusProjectDO.getBillnum())));
                        }
                    }
                }
            }
            focusKpiVOList.get(i).setLendnum();
        }
        return focusKpiVOList;
    }

    public List<FocusKpiVO> getFocusKpiDOMonthByFocusKpiDO(String userId, Integer index) {
        FocusProjectDO searchProjectDO = new FocusProjectDO();
        searchProjectDO.setCoid(userId);
        List<FocusProjectDO> focusProjectDOlist = focusProjectMapper.getFocusProjectDOKPI(searchProjectDO);
        FocusNoticeDO focusNoticeDO = focusNoticeMapper.getFocusNotice();
        List<FocusKpiVO> focusKpiVOList = new ArrayList<FocusKpiVO>();
        List<FocusUserDO> focusUserDOList = focusUserMapper.getFoucUserList();
        Map<String ,FocusUserDO> stringMap = new HashMap<String ,FocusUserDO>();
        for(FocusProjectDO focusProjectDO : focusProjectDOlist){
//            System.out.println("billtime:"+focusProjectDO.getBilltime());
//            System.out.println("index:"+index);
            if(DateUtil.isSameMonthWithToday(DateUtil.plusDay(focusNoticeDO.getHuikuan(), focusProjectDO.getBilltime()), index)){

                for(FocusUserDO focusUserDO : focusUserDOList) {
                    if(focusUserDO.getId().equals(focusProjectDO.getCoid())) {
                        stringMap.put(focusProjectDO.getCoid(), focusUserDO);
                    }
                }
            }
        }
        for (String in : stringMap.keySet()) {
            FocusUserVO focusUserVO = new FocusUserVO(stringMap.get(in));
            FocusKpiVO focusKpiVO = new FocusKpiVO(null,focusUserVO);
            focusKpiVOList.add(focusKpiVO);
        }
        for(int i =0 ;i<focusKpiVOList.size();i++){
            for(FocusProjectDO focusProjectDO : focusProjectDOlist){
                if(DateUtil.isSameMonthWithToday(DateUtil.plusDay(focusNoticeDO.getHuikuan(), focusProjectDO.getBilltime()), index)){

                    if(focusProjectDO.getCoid().equals(focusKpiVOList.get(i).getUser().getId())){
                        System.out.println("--------"+true);
                        //计划回款金额 60天后
                        focusKpiVOList.get(i).setKipnum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getKipnum()) + CommonUtil.stringToDouble(focusProjectDO.getPonum())));
                        focusKpiVOList.get(i).setBillnum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getBillnum()) + CommonUtil.stringToDouble(focusProjectDO.getBillnum())));
                        if(!StringUtil.isNullOrEmpty(focusProjectDO.getRertime())){
                            //已经回款金额
                            focusKpiVOList.get(i).setDoneponum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getDoneponum()) + CommonUtil.stringToDouble(focusProjectDO.getBillnum())));
                        }
                    }
                }
            }
            focusKpiVOList.get(i).setLendnum();
        }
        return focusKpiVOList;
    }

    public List<FocusKpiVO> getFocusKpiDOYearByFocusKpiDO(String userId, String year) {
        FocusProjectDO searchProjectDO = new FocusProjectDO();
        searchProjectDO.setCoid(userId);
        if(!StringUtil.isNullOrEmpty(year)) {
            searchProjectDO.setPryear(year);
        }
        List<FocusProjectDO> focusProjectDOlist = focusProjectMapper.getFocusProjectDOKPI(searchProjectDO);

        List<FocusKpiVO> focusKpiVOList = new ArrayList<FocusKpiVO>();
        List<FocusUserDO> focusUserDOList = focusUserMapper.getFoucUserList();
        Map<String ,FocusUserDO> stringMap = new HashMap<String ,FocusUserDO>();
        for(FocusProjectDO focusProjectDO : focusProjectDOlist){
            for(FocusUserDO focusUserDO : focusUserDOList) {
                if(focusUserDO.getId().equals(focusProjectDO.getCoid())) {
                    stringMap.put(focusProjectDO.getCoid()+"_"+focusProjectDO.getPryear(), focusUserDO);
                }
            }
        }
        for (String in : stringMap.keySet()) {
            FocusUserVO focusUserVO = new FocusUserVO(stringMap.get(in));
            FocusKpiVO focusKpiVO = new FocusKpiVO(null,focusUserVO);
            focusKpiVO.setPryear(in.split("_")[1]);
            focusKpiVOList.add(focusKpiVO);
        }
        for(int i =0 ;i<focusKpiVOList.size();i++){
            for(FocusProjectDO focusProjectDO : focusProjectDOlist){
                    if(focusProjectDO.getCoid().equals(focusKpiVOList.get(i).getUser().getId()) && focusProjectDO.getPryear().equals(focusKpiVOList.get(i).getPryear())){
                        focusKpiVOList.get(i).setKipnum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getKipnum()) + CommonUtil.stringToDouble(focusProjectDO.getPonum())));
                        focusKpiVOList.get(i).setBillnum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getBillnum()) + CommonUtil.stringToDouble(focusProjectDO.getBillnum())));
                        if(!StringUtil.isNullOrEmpty(focusProjectDO.getRertime())){
                            //已经回款金额
                            focusKpiVOList.get(i).setDoneponum(CommonUtil.doubletoStr(CommonUtil.stringToDouble(focusKpiVOList.get(i).getDoneponum()) + CommonUtil.stringToDouble(focusProjectDO.getBillnum())));
                        }
                    }
            }
            focusKpiVOList.get(i).setLendnum();
        }
        return focusKpiVOList;
    }

    public FocusKpiDO getprojectListSum(List<FocusProjectVO> projectList) {
        FocusKpiDO focusKpiDO = new FocusKpiDO();
        double  sumPlan = 0.0;
        double  sumReal = 0.0;
        double  sumBill = 0.0;
        for (FocusProjectVO focusProjectVO : projectList){
            sumPlan = sumPlan+CommonUtil.stringToDouble(focusProjectVO.getPlannum());
            sumReal = sumReal+CommonUtil.stringToDouble(focusProjectVO.getPonum());
            sumBill = sumBill+CommonUtil.stringToDouble(focusProjectVO.getBillnum());
        }
        //计划金额
        focusKpiDO.setKipnum(CommonUtil.doubletoStr(sumPlan));
        focusKpiDO.setDoneponum(CommonUtil.doubletoStr(sumReal));
        focusKpiDO.setBillnum(CommonUtil.doubletoStr(sumBill));
        focusKpiDO.setLendnum();

        return focusKpiDO;
    }
}